####################################################################################
## 得到上游处理好的文件
## 去除存在污染的样本
## vcf、maf、seg、mosdepth、msi
sh ${scripts_path}/module/get_genomic_file.sh

## 纯度低于0.1的样本，没有CNV改变，纯度赋值为1
cat ${Titan_path}/Purity_titan.final.tsv | awk -F'\t' '{OFS="\t"}{if($2 < 0.1){$2=1}print}' \
> ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv

## 合并所有质控文件
${Rscript} ${scripts_path}/combineQC.R \
--msi_file ${Qc_path}/All_Msi.tsv \
--depth_file ${Qc_path}/Depth_summary.qualimap.txt \
--info_file ${config_path}/tumor_normal.class.bk.list \
--mutNum_file ${Qc_path}/Vcf_QC.list \
--out_file ${Qc_path}/Summary_Qc.tsv

## 合并纯度
${Rscript} ${scripts_path}/combinePurity.R \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--info_file ${config_path}/tumor_normal.class.bk.list \
--out_file ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## 质控MSI的样本
## https://github.com/vanallenlab/MSIsensor/blob/master/README.md
## https://www.advancesinmolecularpathology.com/article/S2589-4080(21)00016-8/pdf
## Tumor和Normal配对的时候，MSI>3.5认为MSI-High
## 去除MSI-HIGH的样本
## 12个人的胃癌样本
## 11个人MSI>3.5
msi_high_sample=`cat ${Qc_path}/Summary_Qc.tsv | grep -v Tumor | awk -F'\t' '{if($8 > 10)print $2}' | sort -u |\
tr '\n' '|' | sed 's/|$//'`
## 1个人突变数量极高3680
## 有POLL、POLE、POLR2A、POLD1的CDS突变,MSI为2.93
## 也认为是超突变
msi_high_sample=`echo "${msi_high_sample}|JRGC00009"`

## 以下四个Tumor的样本,突变数量不到120个，纯度低于0.1
lowMutTumorSample="S37|JZGC00762|JZGC00750|S34"
## S37为DGC，去除以后剩下的为IM和DGC
## S34为IGC，原类型为IM + IGC + DGC，去除还存在IGC
cat ${config_path}/tumor_normal.class.bk.list | grep -v -E -w ${msi_high_sample} | grep -v -E -w ${lowMutTumorSample} |\
awk -F'\t' '{OFS="\t"}{if($1=="JZGC00580"){$6="IM + IGC"}print}' \
> ${config_path}/tumor_normal.class.list

## JZGC00580的类别进行替换
## IM+IGC+DGC替换为IM+IGC
cp ${Qc_path}/Summary_Qc.tsv ${Qc_path}/Summary_Qc.raw.tsv
cp ${Qc_path}/Summary_Qc.addPurity.tsv ${Qc_path}/Summary_Qc.addPurity.raw.tsv

cat ${Qc_path}/Summary_Qc.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.tsv
cat ${Qc_path}/Summary_Qc.addPurity.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## Tree的文件，S65和JZGC00580各自去除一个样本
cat ${config_path}/tumor_normal.list | grep -E -w "S65|JZ580P1" | grep -v Normal | awk -F, '{print $2}' | sort -u | xargs -P 20 -I Normal sh -c '
echo Normal
sh ${scripts_path}/tree/Treeomics_CombineVcf_v4.sh Normal ${config_path}
'

####################################################################################
## 不考虑class，最标准的tumor+normal的格式
cat ${config_path}/tumor_normal.class.list | awk -F'\t' '{OFS=","}{print $3,$2}' \
> ${config_path}/tumor_normal.list

## IM的样本
im_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4=="IM")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

## GC的样本
gc_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4~"GC")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

cat ${config_path}/tumor_normal.list > ${config_path}/tumor_normal.all.list
cat ${config_path}/tumor_normal.list | grep -E -w "${im_sample}|Tumor" > ${config_path}/tumor_normal.precancer.list
cat ${config_path}/tumor_normal.list | grep -E -w "${gc_sample}|Tumor" > ${config_path}/tumor_normal.cancer.list

####################################################################################
#### 整理基线
## 整理年龄、性别、吸烟、饮酒等信息
## 注释整体和CDS的覆盖区域、倍体、纯度、质控信息
## 最新的添加了MSI患者的信息
<<EOF
${Rscript_mutationTime} ${scripts_path}/CompareBaseLine.R \
--tumor_list ${config_path}/tumor_normal.class.list \
--baseline_file ${config_path}/STAD_MutipleReigon_baseline.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--burden_all_file ${Qc_path}/Burden.coverage10x.Autosomal.txt \
--burden_cds_file ${Qc_path}/Burden.coverage10x.Autosomal.cds.txt \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--out_dir ${config_path}
EOF

####################################################################################
## NMU的MAF文件得到
use_type=("cancer"  "precancer")
for class in ${use_type[@]}
do
echo $class
sh ${scripts_path}/module/mutsig2cv_coding.class.sh ${class}
sh ${scripts_path}/module/mutsig2cv_coding.class.MSI.sh ${class}
done

####################################################################################
#### CHAT
#### 计算CCF
sh ${scripts_path}/module/computeCCF.sh

####################################################################################
#### pyclone
#### 计算聚类
#### 基于单个样本
sh ${scripts_path}/module/computePyclone.SingleSample.sh

####################################################################################
#### Mutationtime
#### 计算突变时间
sh ${scripts_path}/module/computeMutaionTime.sh

####################################################################################
## 分病理类型合并GGA以后的MAF
export use_type=("all" "cancer"  "precancer")

for class in ${use_type[@]}
do
echo $class
## 头行注释
cat ${maf_path}/*_GGA_Filter_funcotated.maf | grep -v "#" | head -1 > ${maf_path}/All_GGA.${class}.maf
for line in `cat ${config_path}/tumor_normal.${class}.list | grep -v Tumor`
do
Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`
cat ${maf_path}/${Tumor}_${Normal}_GGA_Filter_funcotated.maf | grep -v -E "##|Hugo_Symbol|#version"  | \
awk -F'\t' '{OFS="\t"}{$16=Tumor;print}' Tumor=${Tumor} \
>> ${maf_path}/All_GGA.${class}.maf
done
done